package com.example.singletest.inventory;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RocketMQMessageListener(
        topic = "ORDER_TOPIC",
        consumerGroup = "inventory-group",
        consumeMode = ConsumeMode.CONCURRENTLY,
        messageModel = MessageModel.CLUSTERING,
        maxReconsumeTimes = 5   // 最多重试 5 次
)
public class InventoryListener implements RocketMQListener<String> {
    @Autowired
    private InventoryService inventoryService;
    /*
    * 第一版,直接处理
    * */
    //@Override
    public void onMessage1(String orderId) {
        // 异步处理库存
        inventoryService.reduceStock(orderId);
    }

    /*
     * 第二版,直接处理
     * */
    @Override
    public void onMessage(String orderId) {
        try {
            inventoryService.reduceStock(orderId);
        } catch (Exception e) {
            log.error("库存扣减失败，稍后重试 orderId={}", orderId, e);
            throw new RuntimeException("RECONSUME_LATER");
        }
    }

}
